import { Asset } from "cc";
import { CGameData, initData } from "../../../../app/GameDefine";
import { ILevelSelectUIData } from "../../../../scene/script/ui/LevelSelectUI";
import { LevelHelper, Maths, RandomSeed, TResoucesUrl, _config_, _gameType, _logic, _prop, _timer, _ui, winCenterPostion } from "../../Main";
import { EGameType, IGameTypeLogic, gameTypeModule } from "../define/GameTypeDefine";
import { ELogicDataState, EPropId } from "../define/LogicDefine";



@gameTypeModule.add(EGameType.tenLevel)
export class TenLevelGameType implements IGameTypeLogic {

    public level: LevelHelper<null, null> = null!
    public curLevel = -1

    public get curConfig() {
        return _config_.obj.ten_level_item[this.level.getConverLevel(this.curLevel)]
    }

    constructor() {
        let levelExclude: number[] = []

        let all = Math.floor(_config_.arr.ten_level_item.length / 2)
        for (let i = 0; i < all; i++)
            levelExclude.push(_config_.arr.ten_level_item[i].id)

        this.level = new LevelHelper(
            _gameType.storage,
            -1,
            "ten_level",
            _config_.arr.ten_level_item.length,
            true,
            levelExclude,
            null!
        )
    }

    public preLoadRes(res: TResoucesUrl<Asset>[]) {
        res.push(..._logic.ten.preLoadRes)
    }

    public settingExit() {
        _ui.open(CGameData.LevelSelectUrl, <ILevelSelectUIData>{
            level: this.level,
            configLength: _config_.arr.ten_level_item.length,
            add: true,
        })
    }

    public run(level = this.level.cur) {
        this.curLevel = level
        // this.curLevel = 2

        let config = this.curConfig


        _ui.close(CGameData.LevelSelectUrl)
        _ui.close(initData.uiUrl.index)


        _gameType.logicRun(
            -1,
            () => {
                _logic.ten.create(
                    config.drag_card_add_count,
                    config.is_ten == 1,
                    config.size,
                    config.size_bottom,
                )
            },
            (isWin) => {
                if (isWin) {
                    if (this.level.cur == this.curLevel)
                        this.level.add()

                    _ui.blockTouchEvent(true)
                    // 等待飞行动画
                    _timer.once(this, () => {
                        _ui.blockTouchEvent(false)
                        _ui.open(CGameData.SuccessUrl)
                    }, .5)
                }
                else
                    _ui.open(CGameData.FailUrl)
            }
        )

        _ui.open(CGameData.RunUIUrl, null!, () => {
            if (config.anim === 1)
                _gameType.emit(_gameType.EventType.PLAY_ANIM)
        })
    }


    public checkComplete() {
        if (_logic.ten.dragCount > 0)
            return

        for (let data of _logic.ten.data.datas)
            if (data.state != ELogicDataState.none)
                return
        _gameType.runComplete(true)
    }

    public reset() {
        _gameType.run(_gameType.type, this.curLevel)
    }


    public beforeRun() {
        if (_gameType.getEnterRunCount(EGameType.tenLevel) <= 1)
            return
        let power = _prop.singles.get(EPropId.power)
        power.sub(1)
        let config = _config_.obj.prop[power.id]
        _ui.moveUp(winCenterPostion(), "-1", config.icon_url, null!, 2)
    }

    public hasRun() {
        if (_gameType.getEnterRunCount(EGameType.tenLevel) <= 1)
            return true

        let power = _prop.singles.get(EPropId.power)
        if (!power.has(1)) {
            _ui.tip("体力不足")
            _prop.openGetUI(power)
            return false
        }
        return true
    }

    public revive() {
        _gameType.isRun = true
        _gameType._runComplete.set(_gameType._runComplete._cb)

        if (_logic.ten.hasResetConfig())
            _logic.ten.resetConfig()
        else
            _logic.ten.propRemove()
    }

}